 #define _CRT_SECURE_NO_WARNINGS 1

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> ret;//记录最终结果
        queue<Node*> q;//记录每一层节点
        //处理边界情况
        if (root == nullptr) return ret;

        q.push(root);
        while (q.size())
        {
            vector<int> tmp;//记录当前层的所有节点
            int sz = q.size();//记录当前层的节点个数
            for (int i = 0; i < sz; i++)
            {
                Node* t = q.front();//提取当前节点
                q.pop();
                tmp.push_back(t->val);
                for (Node* child : t->children)//遍历当前节点的孩子节点，并将其放进队列中
                {
                    if (child != nullptr)
                        q.push(child);
                }
            }

            ret.push_back(tmp);
        }
        return ret;

    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if (root == nullptr) return ret;

        queue<TreeNode*> q;
        q.push(root);

        int level = 1;//记录层数，用于标记该层是否要逆序
        while (q.size())
        {
            int sz = q.size();
            vector<int> tmp;
            for (int i = 0; i < sz; i++)
            {
                TreeNode* t = q.front();
                q.pop();
                tmp.push_back(t->val);
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }

            if (level % 2 == 0) reverse(tmp.begin(), tmp.end());
            level++;
            ret.push_back(tmp);
        }

        return ret;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        unsigned int ret = 1;
        vector<pair<TreeNode*, unsigned int>> q;//用数组模拟队列

        q.push_back({ root, 1 });
        while (q.size())
        {
            //先更新当前层的高度
            auto& [x1, y1] = q[0];
            auto& [x2, y2] = q.back();

            ret = max(ret, y2 - y1 + 1);

            //让下一层入队列
            vector<pair<TreeNode*, unsigned int>> tmp;
            for (auto& [x, y] : q)
            {
                if (x->left) tmp.push_back({ x->left, y * 2 });
                if (x->right) tmp.push_back({ x->right, y * 2 + 1 });
            }

            q = tmp;
        }

        return ret;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> ret;
        if (root == nullptr) return ret;

        queue<TreeNode*> q;
        q.push(root);

        while (q.size())
        {
            int sz = q.size();
            int tmp = INT_MIN;
            for (int i = 0; i < sz; i++)
            {
                TreeNode* t = q.front();
                q.pop();
                tmp = max(tmp, t->val);
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }
            ret.push_back(tmp);
        }
        return ret;
    }
};